package com.hung.view.page;

import com.hung.pojo.Account;
import com.hung.pojo.Part;
import com.hung.service.serviceImpl.PartServiceImpl;
import com.hung.view.login.LoginPage;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;

/**
 * 主页面
 *
 * @author Hung
 */
public class MainPage {
    JFrame jf = new JFrame("恰瓜系统");


    public void init(Account account) {
        jf.setTitle("欢迎来到恰瓜系统,你好" + account.getName() + "!");
        jf.setSize(400, 600);
        jf.setLocationRelativeTo(null);
        //jf.setResizable(false);

        //设置菜单栏
        JMenuBar menuBar = new JMenuBar();
        JMenu setting = new JMenu("设置");
        JMenuItem exchangeAccount = new JMenuItem("切换账号");
        JMenuItem exit = new JMenuItem("退出程序");

        //切换账号添加功能
        exchangeAccount.addActionListener(actionEvent -> {
            new LoginPage().init();
            jf.dispose();
        });
        //退出程序添加功能
        exit.addActionListener(actionEvent -> System.exit(0));

        setting.add(exchangeAccount);
        setting.add(exit);
        menuBar.add(setting);
        jf.setJMenuBar(menuBar);

        //设置分割面板
        JSplitPane splitPane = new JSplitPane();
        splitPane.setContinuousLayout(true);
        splitPane.setDividerLocation(150);
        splitPane.setDividerSize(7);

        //设置左侧树
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("恰瓜系统");
        DefaultMutableTreeNode part = new DefaultMutableTreeNode("瓜圈");
        DefaultMutableTreeNode userManage = new DefaultMutableTreeNode("个人设置");
        DefaultMutableTreeNode collectManage = new DefaultMutableTreeNode("收藏合集");
        DefaultMutableTreeNode praiseManage = new DefaultMutableTreeNode("点赞合集");
        DefaultMutableTreeNode reportManage = new DefaultMutableTreeNode("举报管理");
        DefaultMutableTreeNode partManage = new DefaultMutableTreeNode("瓜圈管理");

        //添加结点
        root.add(part);
        root.add(userManage);
        root.add(collectManage);
        root.add(praiseManage);

        //管理员特有
        if (account.getRid() == 1) {
            root.add(reportManage);
            root.add(partManage);
        }

        JTree jTree = new JTree(root);
        jTree.setSelectionRow(2);
        splitPane.setRightComponent(new PersonComponent(account, jf));

        jTree.addTreeSelectionListener(selectionEvent -> {
            //得到结点对象
            Object lastPathComponent = selectionEvent.getNewLeadSelectionPath().getLastPathComponent();

            if (lastPathComponent.equals(part)) {
                //弹出选择框，选择瓜圈，根据选择进入不同瓜圈
                int option = choosePart();
                splitPane.setRightComponent(new PartComponent(option+1, account, jf, splitPane));
                splitPane.setDividerLocation(150);
            } else if (lastPathComponent.equals(userManage)) {
                splitPane.setRightComponent(new PersonComponent(account, jf));
                splitPane.setDividerLocation(150);
            } else if (lastPathComponent.equals(collectManage)) {
                splitPane.setRightComponent(new FavoriteComponent(account));
                splitPane.setDividerLocation(150);
            } else if (lastPathComponent.equals(praiseManage)) {
                splitPane.setRightComponent(new PraiseComponent(account));
                splitPane.setDividerLocation(150);
            } else if (lastPathComponent.equals(reportManage)) {
                splitPane.setRightComponent(new ReportComponent());
            } else if (lastPathComponent.equals(partManage)) {
                splitPane.setRightComponent(new PartManageComponent());
            }

        });
        splitPane.setLeftComponent(jTree);

        jf.add(new JScrollPane(splitPane));

        jf.pack();
        jf.setVisible(true);
    }

    /**
     * 瓜圈选择组件
     *
     * @return
     */
    public int choosePart() {
        //查询所有瓜圈
        PartServiceImpl partService = new PartServiceImpl();
        List<Part> parts = partService.queryAllParts();
        String[] options = new String[parts.size()];
        for (int i = 0; i < parts.size(); i++) {
            Part part = parts.get(i);
            options[i] = part.getName();
        }
        int optionDialog = JOptionPane.showOptionDialog(jf, "请选择你要进去的瓜圈", "瓜圈选择", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        return optionDialog;
    }
}
